package channel;

import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

class ChannelServer extends Thread {

    private ServerSocket ss;
    private ArrayList<ChannelItem> sockets;
    private boolean running;

    private String motd;

    public ChannelServer(ServerSocket ss, ArrayList<ChannelItem> sockets, String motd) {
        this.ss = ss;
        this.sockets = sockets;
        this.motd = motd;
    }

    @Override
    public void run() {
        running = true;
        try {
            ExecutorService exec = Executors.newCachedThreadPool();
            while (running) {
                Socket socket = ss.accept();
                ChannelInterface newInt = new ChannelInterface(socket, sockets, motd);
                exec.submit(newInt);
                socket.setSoTimeout(35000);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void quit() {
        running = false;
        interrupt();
    }
}
